package com.ruov.plugin.wx.offiaccount.config;

import cn.hutool.core.collection.CollUtil;
import com.ruov.plugin.wx.config.WxProperties;
import com.ruov.plugin.wx.offiaccount.handler.LogHandler;
import com.ruov.plugin.wx.offiaccount.handler.MsgHandler;
import com.ruov.plugin.wx.offiaccount.handler.ScanHandler;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.Objects;
import java.util.stream.Collectors;

import static me.chanjar.weixin.common.api.WxConsts.XmlMsgType.EVENT;

/**
 * @author wjsmc
 * @date 2025/4/18 14:08
 * @description
 **/
@Component
@AllArgsConstructor
public class OffiaccountConfig {

    private final WxProperties wxProperties;
    private final LogHandler logHandler;
    private final MsgHandler msgHandler;
    private final ScanHandler scanHandler;

    @Bean
    public WxMpService wxMpService() {
        WxMpService service = new WxMpServiceImpl();
        if (CollUtil.isNotEmpty(wxProperties.getOffiaccount())) {
            service.setMultiConfigStorages(wxProperties.getOffiaccount()
                    .stream().map(a -> {
                        WxMpDefaultConfigImpl configStorage;
                        configStorage = new WxMpDefaultConfigImpl();
                        configStorage.setAppId(a.getAppId());
                        configStorage.setSecret(a.getSecret());
                        configStorage.setToken(a.getToken());
                        configStorage.setAesKey(a.getAesKey());
                        return configStorage;
                    }).collect(Collectors.toMap(WxMpDefaultConfigImpl::getAppId, a -> a, (o, n) -> o)));
        }
        return service;
    }

    @Bean
    public WxMpMessageRouter messageRouter(WxMpService wxMpService) {
        final WxMpMessageRouter newRouter = new WxMpMessageRouter(wxMpService);
        newRouter.rule().handler(this.logHandler).next();
//
//        // 接收客服会话管理事件
//        newRouter.rule().async(false).msgType(EVENT).event(KF_CREATE_SESSION)
//                .handler(this.kfSessionHandler).end();
//        newRouter.rule().async(false).msgType(EVENT).event(KF_CLOSE_SESSION)
//                .handler(this.kfSessionHandler).end();
//        newRouter.rule().async(false).msgType(EVENT).event(KF_SWITCH_SESSION)
//                .handler(this.kfSessionHandler).end();
//
//        // 门店审核事件
//        newRouter.rule().async(false).msgType(EVENT).event(POI_CHECK_NOTIFY).handler(this.storeCheckNotifyHandler).end();
//
//        // 自定义菜单事件
//        newRouter.rule().async(false).msgType(EVENT).event(WxConsts.EventType.CLICK).handler(this.menuHandler).end();
//
//        // 点击菜单连接事件
//        newRouter.rule().async(false).msgType(EVENT).event(WxConsts.EventType.VIEW).handler(this.nullHandler).end();
//
//        // 关注事件
//        newRouter.rule().async(false).msgType(EVENT).event(SUBSCRIBE).handler(this.subscribeHandler).end();
//
//        // 取消关注事件
//        newRouter.rule().async(false).msgType(EVENT).event(UNSUBSCRIBE).handler(this.unsubscribeHandler).end();
//
//        // 上报地理位置事件
//        newRouter.rule().async(false).msgType(EVENT).event(WxConsts.EventType.LOCATION).handler(this.locationHandler).end();
//
//        // 接收地理位置消息
//        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.LOCATION).handler(this.locationHandler).end();
//
//        // 扫码事件
        newRouter.rule().async(false).msgType(EVENT)
                .eventRegex("("+WxConsts.EventType.SCAN+"|"+WxConsts.EventType.SUBSCRIBE+")")
                .handler(this.scanHandler).end();
        newRouter.rule().async(false).handler(this.msgHandler).end();

        return newRouter;
    }


}
